using System.Collections;
using System.Collections.Generic;
using AppGameState;
using UnityEngine;
using UnityEngine.Serialization;


public class RectBoundInfo
{

    public RectBoundInfo(Vector3[] bound, Vector2 center)
    {
        
        this.left = bound[0].x;
        this.down = bound[0].y;
        this.top = bound[1].y;
        this.right = bound[2].x;
        this.center = center;
    }
    
    public float top;
    public float down;
    public float left;
    public float right;    
    public Vector2 center;    
}

public class RectBounds
{
    public RectBoundInfo soldier_place_top;
    public RectBoundInfo soldier_place_down;
    public RectBoundInfo role_place_top;
    public RectBoundInfo role_place_down;
    public RectBoundInfo middle_place;
    public RectBoundInfo player_hands_place;
    public RectBoundInfo enemy_hands_place;
}

public class ScreenPlaceManager : MonoBehaviour
{
    public GameObject soldier_place_top;
    public GameObject soldier_place_down;
    
    [HideInInspector] public bool is_ready = false;

    public Vector2 center_of_soldier_top;
    public Vector2 center_of_soldier_down;
    public Vector2 center_of_soldier_place;
    
    [HideInInspector] public List<Vector3[]> soldier_place_info_ls;  // left_bottom, left_top, right_top, right_bottom

    // public List<RectBoundInfo> soldier_rect_bound_info_ls;
    public RectBounds rect_bounds = new RectBounds();
    
    public bool debug = false;

    public Vector3[] get_bound_for_ui_obj(GameObject ui_obj)
    {
        BdPackage.BdUtils.assert(ScreenPositionTools.check_is_ui_obj(ui_obj));
        
        RectTransform rectTransform = ui_obj.GetComponent<RectTransform>();
        Rect rect = rectTransform.rect;
        // 将UI的四个角转换为世界坐标
        Vector3[] bound = new Vector3[4];
        // List<Vector3> bound = new List<Vector3>();
        rectTransform.GetWorldCorners(bound);

        // BdPackage.BdUtils.log("--- get_bound_for_ui_obj:", soldier_place_top.name, "---bound:", BdPackage.BdUtils.conv_obj_to_list(bound));
        
        // var (left_bottom, left_top, right_top, right_bottom, center) = (bound[0], bound[1], bound[2], bound[3], bound[4]);
        return bound;
    }

    public Vector3 get_center_of_bound(Vector3[] bound)
    {
        var (left_bottom, left_top, right_top, right_bottom) = (bound[0], bound[1], bound[2], bound[3]);
        Vector3 center = (left_bottom + right_top) / 2;
        return center;
    }

    // public (float, float, Vector3) get_left_center_right_for_ui_obj(GameObject ui_obj)
    // {
    //     return get_wh_and_center_of_bound(get_bound_for_ui_obj(ui_obj));
    // }
    
    public (float, float, Vector3) get_wh_and_center_of_bound(Vector3[] bound)
    {
        var (left_bottom, left_top, right_top, right_bottom) = (bound[0], bound[1], bound[2], bound[3]);
        var center = get_center_of_bound(bound);
        
        var width = (right_top.x - left_top.x) / 2;
        var height = (right_top.y - right_bottom.y) / 2;
        
        return (width, height, center);
    }

    public RectBoundInfo get_rect_bound_info(GameObject ui_obj)
    {
        BdPackage.BdUtils.assert(ScreenPositionTools.check_is_ui_obj(ui_obj));
        var bound = get_bound_for_ui_obj(ui_obj);
        var center = get_center_of_bound(bound);
        var res = new RectBoundInfo(bound, center);
        // BdPackage.BdUtils.log($"--- ui_obj: {ui_obj.name}, bound: {bound}, center: {center}");
        return res;
    }
    
    IEnumerator calculate_positions()
    {
        if (is_ready)
            yield break;

        yield return new WaitForSeconds(0.1f);

        soldier_place_info_ls = new List<Vector3[]>();
        
        var bound_top = get_bound_for_ui_obj(soldier_place_top);
        // var (left_bottom, left_top, right_top, right_bottom, center) = (bound[0], bound[1], bound[2], bound[3], bound[4]); 

        soldier_place_info_ls.Add(bound_top);
        
        var bound_down = get_bound_for_ui_obj(soldier_place_down);
        
        soldier_place_info_ls.Add(bound_down);

        // BdPackage.BdUtils.log("~~~ soldier_place_info_ls:", soldier_place_info_ls);

        center_of_soldier_top = get_center_of_bound(bound_top);
        center_of_soldier_down = get_center_of_bound(bound_down);
        center_of_soldier_place = (center_of_soldier_top + center_of_soldier_down) / 2;

        
        // var (left_bottom, left_top, right_top, right_bottom, center) = (bound[0], bound[1], bound[2], bound[3], bound[4]); 
        var bound_top_info = new RectBoundInfo(bound_top, center_of_soldier_top);
        rect_bounds.soldier_place_top = bound_top_info;
        
        var bound_down_info = new RectBoundInfo(bound_down, center_of_soldier_down);
        rect_bounds.soldier_place_down = bound_down_info;

        rect_bounds.role_place_top = get_rect_bound_info(role_place_top);
        rect_bounds.role_place_down = get_rect_bound_info(role_place_down);
        rect_bounds.middle_place = get_rect_bound_info(middle_place);
        rect_bounds.enemy_hands_place = get_rect_bound_info(enemy_hands_place);
        rect_bounds.player_hands_place = get_rect_bound_info(player_hands_place);
        
        // BdPackage.BdUtils.log("~~~ bound_top:", bound_top);
        // BdPackage.BdUtils.log("~~~ center_of_soldier_top:", center_of_soldier_top);
        // BdPackage.BdUtils.log("~~~ center_of_soldier_down:", center_of_soldier_down);
        // BdPackage.BdUtils.log("~~~ center_of_soldier_place:", center_of_soldier_place);

        is_ready = true;
    }

    public GameObject role_place_top;
    public GameObject role_place_down;
    public GameObject middle_place;
    public GameObject enemy_hands_place;
    public GameObject player_hands_place;
    
    public static ScreenPlaceManager instance;

    private void Awake()
    {
        if (instance != null)
            Destroy(this);
        instance = this;
        is_ready = false;
    }

    void Start()
    {
        BdPackage.BdUtils.assert(soldier_place_top != null);
        BdPackage.BdUtils.assert(soldier_place_down != null);

        BdPackage.BdUtils.assert(ScreenPositionTools.check_is_ui_obj(soldier_place_top));
        BdPackage.BdUtils.assert(ScreenPositionTools.check_is_ui_obj(soldier_place_down));

        StartCoroutine(calculate_positions());
    }

    void Update()
    {
        
    }

    private void OnGUI()
    {
        if (!debug)
            return;
        
        // 定义按钮的宽度和高度
        float buttonWidth = 150f;
        float buttonHeight = 50f;

        // 计算按钮区域的位置
        float xPosition = Screen.width - buttonWidth - 10f; // 距离屏幕右侧 10 像素
        float yPosition = 10f; // 距离屏幕顶部 10 像素

        // 创建一个区域
        GUILayout.BeginArea(new Rect(xPosition, yPosition, buttonWidth, buttonHeight));
        // 在区域内创建按钮
        if (GUILayout.Button("计算 soldier_place_top 区域", GUILayout.Width(buttonWidth), GUILayout.Height(buttonHeight)))
        {
            StartCoroutine(calculate_positions());
        }
        GUILayout.EndArea();
        
        GUILayout.BeginArea(new Rect(xPosition, yPosition + buttonHeight * 1.5f, buttonWidth, buttonHeight));
        if (GUILayout.Button("获取 current_state", GUILayout.Width(buttonWidth), GUILayout.Height(buttonHeight)))
        {
            BdPackage.BdUtils.log("--- current_state:", GameStateManager.instance?.get_current_state());
        }
        GUILayout.EndArea();
    }
}
